<?php
/**
 * Klasse zur Installation von Datensaetzen, welche in CSV gespeichert sind
 *
 * HINWEIS:
 * Die CSV-Datei muss in der ersten Zeile die Relationsnamen (Feldnamen) beinhalten!
 *
 * @author		Joachim Ruf
 * @copyright	Copyright (c) 2003-2010 Loresoft Software (http://www.loresoft.de)
 * @license		http://yapary.loresoft.de/lizenzbedingungen     New BSD License
 *
 * */


Class Yapary_Database_CsvInstall Extends Yapary_String
{
    private $oSql         = NULL;
    private $oString      = NULL;
    private $filePath     = NULL;
    private $arrContent   = array();
    private $encodingType = NULL;
    private $delimiter    = NULL;
    private $enclosure    = NULL;


	function __construct (Yapary_Database_Sql $_oSql, $_dbTableName, $_filePath, $_encodingType = 'ISO-8859-1', $_delimiter = ';', $_enclosure = '"')
	{
	    if (!file_exists($_filePath)) {
	        trigger_error('File <b>'.$_filePath.'</b> does not exist', E_USER_ERROR);
	    }

	    $this->oSql         = $_oSql;
	    $this->oString      = new Yapary_String();
	    $this->dbTableName  = $_dbTableName;

	    $this->filePath     = $_filePath;
	    $this->encodingType = $_encodingType;
	    $this->delimiter    = $_delimiter;
        $this->enclosure    = $_enclosure;
	}


	/**
	 * Laedt die Daten in die Datenbanktabelle
	 * @param $arrRelationName	: Namen der Relationen in der Folge, wie sie in der CSV-Datei stehen
	 * */
	public function install ($iFrom = NULL, $iTo = NULL)
	{
	    $i           = 0;
        $success     = 0;
        $dbTableName = $this->dbTableName;
        $paramKey    = '';
        $paramVal    = '';
        $arrParamVal = array();


        $handle = fopen ($this->filePath, 'r');
        if (($arrKey = fgetcsv ($handle, 4096, $this->delimiter, $this->enclosure)) !== false) {
            $paramKey = implode(',', $arrKey);
        }
        while (($arrTmp = fgetcsv ($handle, 4096, $this->delimiter, $this->enclosure)) !== false) {
			$i++;
			$paramVal = '';

			if ($iFrom != NULL && $iFrom > $i) {
			    continue;
			}
			if ($iTo != NULL && $iTo < $i) {
			    break;
			}

			// Werte aneinander fuegen und Korrektur fuer letzten Komma durchfuehren
            $paramVal = implode('","', $arrTmp);
            $arrParamVal[] = $this->oString->mbStringConvert('("'.$paramVal.'")');
		    $success++;

		    if ($i%5000 == 0) { // Nach 5000 Datensaetzten abspeichern und Feld resetten
                $paramVal = implode(',', $arrParamVal);
                $arrParamVal = array();
                $this->save($paramKey, $paramVal, $success);
		    }
        }

        $paramVal = implode(',', $arrParamVal);
        $this->save($paramKey, $paramVal, $success);
        fclose ($handle);
        return $success;
	}


	/**
	 * Datenabspeicherung
	 * */
	private function save ($paramKey, $paramVal, $i)
	{
        $dbTableName = $this->dbTableName;

        if ($this->oSql->request("INSERT INTO $dbTableName ($paramKey) VALUES $paramVal")) {
            return true;
		} else {
		    trigger_error('Error while install dataset '.$i.'', E_USER_NOTICE);
		    echo $this->oSql->getQuery();
            return false;
		}
	}

}


?>